﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DaInterFace;
using DaInterFace.SYN;
using DaInterFace.SYN.E01;
//using System.Guid;

namespace E01
{
    /// <summary>
    /// 同步供应商
    /// </summary>
    public class A33 : ISYNChildSet
    {
        public void Code(ISYN handler, CodeResult result, SYNRight user, SerialType st, String parameter)
        {
            E01A33P P = (E01A33P)handler.Serial.Deserialize(st, typeof(E01A33P), parameter);

            try
            {
                SqlParameter[] sps = new SqlParameter[3];
                sps[0] = new SqlParameter("@ID", SqlDbType.NVarChar, 50);
                sps[0].Value = P.ID;
                sps[1] = new SqlParameter("@Number", SqlDbType.NVarChar, 50);
                sps[1].Value = P.Number;
                sps[2] = new SqlParameter("@Name", SqlDbType.NVarChar, 50);
                sps[2].Value = P.Name;

                String sql =" Begin tran \r\n" +
                            " declare @flag int  \r\n" +
                            " declare @FID int \r\n" +
                            " declare @result int \r\n" +
                            " set @flag = 0 \r\n" +
                            " set @result = 0 \r\n" +
                            " select @FID = isnull(FItemID,-10) from t_Supplier where F_102 = @ID \r\n" +
                            " if(@FID is not null and @FID > 0) \r\n" +
                            " Begin \r\n" +
                            " 	update t_Supplier set FShortNumber = @Number,FNumber = @Number,FName = @Name,F_102=@ID where FItemID = @FID \r\n" +
                            " 	set @flag = @flag + @@ERROR \r\n" +
                            " 	update t_Item set FName = @Name,FNumber=@Number,FShortNumber=@Number,FFullNumber=@Number  where FItemID = @FID \r\n" +
                            " 	set @flag = @flag + @@ERROR  \r\n" +
                            " 	set @result = 1 \r\n" +
                            " END \r\n" +
                            " Else \r\n" +
                            " Begin \r\n" +
                            " 	EXEC GetICMaxNum 't_Item',@FID OUTPUT \r\n" +
                            " 	INSERT INTO t_Item (FItemID,FItemClassID,FParentID,FLevel,FName,FNumber,FShortNumber,FFullNumber,FDetail,UUID,FDeleted) \r\n" +
                            " 	VALUES (@FID,8,0,1,@Name,@Number,@Number,@Number,1,CAST(NEWID() as varchar(36)),0) \r\n" +
                            " 	set @flag = @flag + @@ERROR \r\n" +

                            "   INSERT INTO t_Supplier (FHelpCode,FShortName,FAddress,FStatus,FRegionID,FTrade,FContact,FPhone,FMobilePhone,FFax, \r\n" +
                            "   FPostalCode,FEmail,FBank,FAccount,FTaxNum,FValueAddRate,FCountry,FProvince,Fcorperate,FDiscount, \r\n" +
                            "   FTypeID,FStockIDAssignee,FBr,FRegmark,FLicence,FCyID,FSetID,FAPAccountID,FPreAcctID,FOtherAPAcctID,FPayTaxAcctID,FARAccountID,FPreARAcctID, \r\n" +
                            "   FOtherARAcctID,FfavorPolicy,Fdepartment,Femployee,FlastTradeAmount,FlastRPAmount,FmaxDealAmount,FminForeReceiveRate,FCreditDays,FNameEN,FAddrEn, \r\n" +
                            "   FCIQCode,FRegion,FManageType,FRegsterDate,FAbateDate,FSupplyGrade,FSupplyType,FCompanyType,FAutoCreateMR,FAutoValidateOrderFlag,FSupplierCoroutineFlag, \r\n" +
                            "   FAPFrozenFlag,F_102,FShortNumber,FNumber,FName,FParentID,FItemID)  \r\n" +
                            "   VALUES (NULL,NULL,NULL,1072,0,0,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,17,NULL,NULL,NULL,0,0,0,0,NULL,NULL,0,0,0,0,0, \r\n" +
                            "   0,0,0,0,NULL,0,0,0,0,0,1,0,NULL,NULL,NULL,0,0,NULL,'2100-12-31 00:00:00',0,0,0,0,0,0,0,@ID,@Number,@Number,@Name,0,@FID) \r\n" +
                            " 	set @flag = @flag + @@ERROR  \r\n" +
                            " 	set @result = 2 \r\n" +
                            " End \r\n" +
                            " If @flag = 0  \r\n" +
                            " Begin  \r\n" +
                            " 	commit tran  \r\n" +
                            " 	select 1,@FID,@result \r\n" +
                            " End  \r\n" +
                            " Else  \r\n" +
                            " Begin  \r\n" +
                            " 	rollBack  \r\n" +
                            " 	select 2,@FID,@result  \r\n" +
                            " End  ";

                Recordset rs = handler.Data.ExeProcedure(P.BaseNo, sql, sps);
                if (rs.Enable)
                {
                    if (rs.Data.Tables[0].Rows[0][0].ToString() == "1")
                    {
                        if (rs.Data.Tables[0].Rows[0][2].ToString() == "1")
                        {
                            result.Result = EnumResult.Update;
                            result.Content = rs.Data.Tables[0].Rows[0][1].ToString();
                            result.Message = String.Format("供应商同步完成:{0},{1}", P.Number, result.Result.ToString());
                        }
                        else
                        {
                            result.Result = EnumResult.Insert;
                            result.Content = rs.Data.Tables[0].Rows[0][1].ToString();
                            result.Message = String.Format("供应商同步完成:{0},{1}", P.Number, result.Result.ToString());
                        }
                    }
                    else
                    {
                        result.Result = EnumResult.None;
                        result.Message = rs.Message;
                        result.Message = String.Format("供应商同步失败:{0}", rs.Message);
                    }
                }
                else
                {
                    result.Result = EnumResult.None;
                    result.Message = rs.Message;
                    result.Message = String.Format("供应商同步失败:{0}", rs.Message);
                }
            }
            catch (Exception ex)
            {
                result.Result = EnumResult.None;
                result.Message = ex.Message;
            }
        }
    }

    /// <summary>
    /// 供应商
    /// </summary>
    public class E01A33P
    {
        /// <summary>
        /// 账套编号
        /// </summary>
        public String BaseNo;
        /// <summary>
        /// 三方内码
        /// </summary>
        public String ID;
        /// <summary>
        /// 三方编号
        /// </summary>
        public String Number;
        /// <summary>
        /// 三方名称
        /// </summary>
        public String Name;
        /// <summary>
        /// 上级内码
        /// </summary>
        // public String ParentID;
        /// <summary>
        /// 增值税率
        /// </summary>
        public Decimal ValueAddRate;
        /// <summary>
        /// 客户简码
        /// </summary>
        public String ShortNumber;
        /// <summary>
        /// 全名
        /// </summary>
        public String FullName;
        /// <summary>
        /// 地址
        /// </summary>
        public String Address;
        /// <summary>
        /// 电话
        /// </summary>
        public String Phone;
        /// <summary>
        /// 传真
        /// </summary>
        public String Fax;
        /// <summary>
        ///联系人
        /// </summary>
        public String Contact;
        /// <summary>
        ///档案编号
        /// </summary>
        public String BillNo;
        /// <summary>
        /// 客户类别 1客户 4供应商
        /// </summary>
        public int ItemClassID;
        /// <summary>
        /// 所处层级
        /// </summary>
       // public int Level;
        /// <summary>
        /// 是否最下层级
        /// </summary>
       // public Boolean Detail;
        /// <summary>
        /// 使用标记
        /// </summary>
        public int UseSign;

    }
}
